package de.fhb.easymealtime.data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.StringTokenizer;

import de.fhb.easymealtime.repository.Einheit;
import de.fhb.easymealtime.repository.rezeptVO;
import de.fhb.easymealtime.repository.zutatVO;

public class ZutatDao extends JdbcDAOBase implements IZutatDAO {

	
	
	@Override
	public zutatVO getZutatByID(int ID) {
		zutatVO vo;
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String sqlStr = "Select * From Zutat WHERE ID like ?";
		try{
			
			con = getConnection();
			ps = con.prepareStatement(sqlStr);
			ps.setInt(1, ID);
			rs = ps.executeQuery();
			vo = (zutatVO)transformSetToVO(rs);
		}catch(SQLException sqle){
			throw new DataAccessException("Error executing");
		} finally {
			try{
				if(ps != null)
					ps.close();
				if(con != null)
					con.close();
			}catch(SQLException sqlee){
				throw new DataAccessException("Can't close connection",sqlee);
			}
		}
		return vo;
	}

	@Override
	public zutatVO getZutatByName(String Name) {
		zutatVO vo = null;
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String sqlStr = "Select * From Zutat WHERE Name like ?";
		
		
		try {
			con = getConnection();
			ps = con.prepareStatement(sqlStr);
			ps.setString(1,Name);
			rs = ps.executeQuery();
			vo = (zutatVO)transformSetToVO(rs);
		} catch (SQLException e) {
			throw new DataAccessException("Error executing");
		}finally{
			try{
				if(ps != null )
					ps.close();
				if(con != null)
					con.close();
			}catch(SQLException sqlee){
				throw new DataAccessException("Can't close connection",sqlee);
			}
			
		}
		
		return null;
	}

	@Override
	public List<zutatVO> getAllZutaten() {
		List<zutatVO> liste = null;
		Connection con =null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String sqlStr ="Select * From Zutat";
		

		try {
			con = getConnection();
			ps = con.prepareStatement(sqlStr);
			rs = ps.executeQuery();
			liste = transformSet(rs);
			
		} catch (SQLException e){
			throw new DataAccessException("Executing Error");
		}finally{
			try{
				if(ps != null)
					ps.close();
				if(con != null)
					con.close();
			
			}catch(SQLException sqlee){
				throw new DataAccessException("Can't close DB",sqlee);
			}
		
	
		}
			
		return liste;
	}

	@Override
	public void createZutat(zutatVO vo) {
		Connection con =null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sqlStr = "Insert Into Zutat(Name,Preis,Menge, Kcal,Einheit) VALUES (?, ?, ?, ?, ?)";
		int key = -1;
		con = getConnection();

		
		int i = 1;

		try {
			ps  = con.prepareStatement(sqlStr,PreparedStatement.RETURN_GENERATED_KEYS);
			ps.setString(i++, vo.getName());
			ps.setInt(i++, vo.getPreis());
			ps.setInt(i++, vo.getMenge());
			ps.setInt(i++, vo.getKcal());
			ps.setString(i++, vo.getEinheit().toString());
			ps.executeUpdate();
			rs = ps.getGeneratedKeys();
		} catch (SQLException e) {
			throw new DataAccessException("error executing",e);

		}finally {
			try {
				if(ps != null)
					ps.close();
				if(con != null)
					ps.close();
			}catch(SQLException sqlee){
				throw new DataAccessException("cant close",sqlee);
			}
		}
		
		
	}
	
	@Override
	protected Object map2VO(ResultSet rs) throws SQLException {
		zutatVO vo = new zutatVO();
		vo.setID(rs.getInt("ID"));
		vo.setName(rs.getString("Name"));
		vo.setMenge(rs.getInt("Menge"));
		vo.setKcal(rs.getInt("Kcal"));
		String einheit = rs.getString("Einheit");
		vo.setPreis(rs.getInt("preis"));
		switch(einheit){
			
		case "G":
			vo.setEinheit(Einheit.G);
		break;
		case "KG":
			vo.setEinheit(Einheit.KG);
		break;
		case "ML":
			vo.setEinheit(Einheit.ML);
		break;
		case "L":
			vo.setEinheit(Einheit.L);
		break;
		
		}
		
		return vo;
	}



}
